- Published on
Rules in Cursor
- Authors
- Name
Control how the Agent model behaves with reusable, scoped instructions.
Rules allow you to provide system-level guidence to the Agent and Cmd-K AI. Think of them as a presistent way to encode context, preference, or workflows for your projects or for yourself.
We support three types of rules.
- Project Rules
- User Rules
- .cursorrules(Legacy)
How rules work
Large language models do not retain memory between completions. Rules solve this by providing presistent, reusable context at the prompt level. 大模型无法在两次完成之间保存记忆,Rules通过在提示词层级提供可重复使用的预存上下文解决这一问题。
When a rule is applied, its contents are included at the start of the model context. This gives the AI consistent guidance whether it is generating code. interpreting edits, or helping with a workflow.
当一个Rules生效,其内容包含在模型内容的开头,这样,无论是生成代码,解释编辑,或者帮助工作流。AI都能获得一致的指导。
Project rules
Project rules live in .cursor/rules Each rule is stored as a file and version-controlled. They can be scoped using path patterns, invoked manually, or included based on relevance.
Project rules 放在 .cursor/rules 下面,每一个 Rule 被存为一个文件,并进行版本管理。这些规则可以使用路径进行范围界定、手动调用,或者根据相关性导入。
Use project rules to:
- Encode domain-specific knowledge about your codebase
- Automate project-specific workflows or templates
- Standardize style or architecture decisions
使用 Project rules 可以做到:
- 编码代码仓库的域名指定知识
- 自动执行工程指定的工作流或模板
- 标准风格或架构决策
Rule structure
Each rule file is written in MDC(.mdc), a lightweight format that supports metadata and content in a single file. Rules supports the following types: 每个规则文件都是以 MDC(.mdc) 格式编写的,这是一种轻量级格式,可在单个文件中支持元数据和内容。规则支持以下类型:
Rule Type | Description |
---|---|
Always | Always included in the model context |
Auto Attached | included when files matching a glob pattern are referenced |
Agent Requested | Rule is avaliable to the AI, which decides whether to include it. Must provide a description |
Manual | Only included when explicitly mentioned using @ruleName |
Rule Type | Description |
---|---|
Always | 总是包含在模型的上下文 |
Auto Attached | 在引用与 glob 模式匹配的文件时包含 |
Agent Requested | 规则可供AI使用,由AI决定是否纳入。必须提供说明 |
Manual | 仅在使用 @ruleName 明确提及时才包括在内 |
Example MDC rule
---
description: RPC Service boilerplate
globs:
alwaysApply: false
---
- Use our internal RPC pattern when defining services
- Always use snake_case for service names.
@service-template.ts
referenced files like @service-template.ts
will be included as additional context when the rule is triggered
触发规则时,@service-template.ts
等引用文件将作为附加上下文被包含在内
Creating a rule
You can create a rule by using the New Cursor Rule command or going to Cursor Settings > Rules. This will create a new rule file in the .cursor/rules directory. From setting you can also see a list of all rules and their status